//搜索相关业务

import 'package:xvideos/hj_model/acg/search_index_model.dart';
import 'package:xvideos/hj_model/home/video_list_resp.dart';
import 'package:xvideos/hj_model/video_model.dart';
import 'package:xvideos/hj_utils/http_manager.dart';

class SearchService {
  /// 获取交友发布tag
  static Future<List<TagsBean>> fetchPublishTags() async {
    final result = await httpManager.fetchResponseByGET('/search/publisher/list',
        param: <String, dynamic>{}, jsonTransformation: (json) => (json['list'] as List?)?.map((e) => TagsBean.fromMap(e)).toList() ?? []);
    return result.data;
  }

  static Future<List<HotTag>> fetchSearchHotTag() async {
    final result = await httpManager.fetchResponseByGET('/search/hotTag',
        param: <String, dynamic>{}, jsonTransformation: (json) => (json['list'] as List?)?.map((e) => HotTag.fromJson(e)).toList());
    return result.data;
  }

  //猜你想要 type: 0v本周， 1本月，2上月 3.最多收藏
  static Future<VideoListResp?> getSearchHotVideos(int pageNumber, int pageSize, int sortType) async {
    final queryParameters = {
      "pageNumber": pageNumber,
      "pageSize": pageSize,
      "type": sortType,
    };
    final result = await httpManager.fetchResponseByGET(
      '/search/hotVid/list',
      param: queryParameters,
      jsonTransformation: (json) => VideoListResp.fromJson(json),
    );
    return result.data;
  }

  /// 获取搜索数据
  ///
  static Future<VideoListResp?> fetchSearchResultByKey(String key, String videoType, {int page = 1, int size = 20, int? sortType}) async {
    final param = {'pageNumber': page, 'pageSize': size, 'keyword': key, 'videoType': videoType, 'sortType': sortType};
    param.removeWhere((key, value) => value == null);

    final result = await httpManager.fetchResponseByGET(
      '/vid/search',
      param: param,
      jsonTransformation: (json) => VideoListResp.fromJson(json),
    );
    return result.data;
  }

  //realm: SP-视频 SHORT-短视频 COVER-帖子 USER-用户
  //sortType: 0综合排序 1最多观看 2最新上架 3最多收藏
  static Future<VideoListResp?> getMediaSearchData(
    String keyWords, {
    int pageNumber = 1,
    int pageSize = 20,
    String realm = 'SP',
    int? sortType,
  }) async {
    final param = {
      'pageNumber': pageNumber,
      'pageSize': pageSize,
      'keyWords': [keyWords],
      'realm': realm,
      'sortType': sortType
    }..removeWhere((key, value) => value == null);

    final result = await httpManager.fetchResponseByPOST(
      '/search/list',
      param: param,
      jsonTransformation: (json) => VideoListResp.fromJson(json),
    );
    return result.data;
  }
}
